﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Globalization;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class diagnosemedical : System.Web.UI.Page
    {
        public static List<string> x = new List<string>();
        public static List<string> y = new List<string>();
        DataTable dt = new DataTable();
        DataTable ds = new DataTable();
        DataTable dx = new DataTable();
        DataTable dy = new DataTable();
     
        protected void Page_Load(object sender, EventArgs e)
        {


            //โชว์ session
            String ShowValue = (string)Session["CID"];
            Label1.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            Label3.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            Label2.Text = ShowValue3;


            String ShowValue4 = (string)Session["CaseNo"];
            Label4.Text = ShowValue4;


                 

            if (!IsPostBack)
            {
                findstaffID();
                BindData();
                BindData2();
            }
        }



        protected void sendsession()
        {
            Session["CID"] = Label1.Text;
            Session["FirstName"] = Label2.Text;
            Session["LastName"] = Label3.Text;
            Session["CaseNo"] = Label4.Text;

            
        }

        //โหลดข้อมูลสตาฟ

        protected void findstaffID()
        {

            

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Staff_id FROM [Employee] where Username=@Username", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@Username", Session["idlogin"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {
                Label5.Text = reader2["Staff_id"].ToString();


            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }

        //สร้างตารางข้อมูลให้เลือก
        protected void BindData()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
           
            x.Clear();
            scon.Open();
            SqlCommand cmd = new SqlCommand("select Name from DisabilityType", scon);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                x.Add(reader["Name"].ToString());
            }
            reader.Close();
            dx.Columns.Add("Name");

            for (int i = 0; i < x.Count(); i++)
            {
                dx.Rows.Add();
                dx.Rows[i]["Name"] = x[i].ToString();
            }
            GridView1.DataSource = dx;
            GridView1.DataBind();
            scon.Close();
           //อ่านว่ามีข้อมูลเก่่าไหมถ้ามีก็อ่านทับ
            
            BindDataread();

        }
        protected void BindData2()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            y.Clear();
            scon.Open();
            SqlCommand cmd = new SqlCommand("select Name from HealthProblem", scon);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                y.Add(reader["Name"].ToString());
            }
            reader.Close();
            dy.Columns.Add("Name");

            for (int i = 0; i < y.Count(); i++)
            {
                dy.Rows.Add();
                dy.Rows[i]["Name"] = y[i].ToString();
            }
            GridView2.DataSource = dy;
            GridView2.DataBind();
            scon.Close();


            BindDataread2();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DisabilityEval();
            HealthEval();
           // sendsession();
        }
        //ใส่ข้อมูลที่เลือกลง Database
        protected void DisabilityEval()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);




            if (dt.Columns.Count <= 0)
            {
                dt.Columns.Add("CID");
                dt.Columns.Add("CaseNo");
                dt.Columns.Add("DisabilityCode");
                dt.Columns.Add("RecorderID");
                dt.Columns.Add("EvalDateTime");
            }
            foreach (GridViewRow row in GridView1.Rows)
            {

              
                scon.Open();
                SqlCommand cmd = new SqlCommand("select DisabilityCode from DisabilityType where Name = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", GridView1.Rows[row.RowIndex].Cells[0].Text);

                CheckBox cb = (CheckBox)row.FindControl("CBX");
                if (cb != null && cb.Checked)
                {
                    SqlDataReader reader2 = cmd.ExecuteReader();
                    if (reader2.Read())
                    {
                        dt.Rows.Add(Label1.Text, Label4.Text, reader2["DisabilityCode"].ToString(), Label5.Text, DateTime.Now.ToString());
                    }
                    else
                    {
                    }
                }
                scon.Close();
            }
            //GridView3.DataSource = dt;
            //GridView3.DataBind();

            

            SqlCommand command2 = new SqlCommand("delete from [DisabilityEvaluation] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", Label1.Text);
            command2.Parameters.AddWithValue("@CaseNo", Label4.Text);
            command2.ExecuteNonQuery();
            scon.Close();
            for (int i = 0; i < dt.Rows.Count; i++)
            {


                


                SqlCommand command = new SqlCommand("INSERT INTO [DisabilityEvaluation] (CID,CaseNo,DisabilityCode,RecorderID,EvalDateTime) VALUES('" + dt.Rows[i]["CID"].ToString() + "','" + dt.Rows[i]["CaseNo"].ToString() + "','" + dt.Rows[i]["DisabilityCode"].ToString() + "','" + dt.Rows[i]["RecorderID"].ToString() + "','" + dt.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                scon.Open();

                command.ExecuteNonQuery();
                scon.Close();


                SqlCommand command3 = new SqlCommand("INSERT INTO [DisabilityEvaluation_log] (CID,CaseNo,DisabilityCode,RecorderID,EvalDateTime) VALUES('" + dt.Rows[i]["CID"].ToString() + "','" + dt.Rows[i]["CaseNo"].ToString() + "','" + dt.Rows[i]["DisabilityCode"].ToString() + "','" + dt.Rows[i]["RecorderID"].ToString() + "','" + dt.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                scon.Open();

                command3.ExecuteNonQuery();
                scon.Close();




            }
        }

        protected void HealthEval()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            if (ds.Columns.Count <= 0)
            {
                ds.Columns.Add("CID");
                ds.Columns.Add("CaseNo");
                ds.Columns.Add("HealthEvalCode");
                ds.Columns.Add("RecorderID");
                ds.Columns.Add("EvalDateTime");
            }
            foreach (GridViewRow row in GridView2.Rows)
            {

                scon.Open();
                SqlCommand cmd = new SqlCommand("select HealthEvalCode from HealthProblem where Name = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", GridView2.Rows[row.RowIndex].Cells[0].Text);

                CheckBox cb = (CheckBox)row.FindControl("CBKX");
                if (cb != null && cb.Checked)
                {
                    SqlDataReader reader2 = cmd.ExecuteReader();
                    if (reader2.Read())
                    {
                        ds.Rows.Add(Label1.Text, Label4.Text, reader2["HealthEvalCode"].ToString(), Label5.Text, DateTime.Now.ToString());
                    }
                    else
                    {
                    }
                }
                scon.Close();
            }
            //GridView4.DataSource = ds;
            //GridView4.DataBind();



            SqlCommand command3 = new SqlCommand("delete from [HealthEvaluation] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command3.Parameters.AddWithValue("@CID", Label1.Text);
            command3.Parameters.AddWithValue("@CaseNo", Label4.Text);
            command3.ExecuteNonQuery();
            scon.Close();



            for (int i = 0; i < ds.Rows.Count; i++)
            {





                SqlCommand command = new SqlCommand("INSERT INTO [HealthEvaluation] (CID,CaseNo,HealthEvalCode,RecorderID,EvalDateTime) VALUES('" + ds.Rows[i]["CID"].ToString() + "','" + ds.Rows[i]["CaseNo"].ToString() + "','" + ds.Rows[i]["HealthEvalCode"].ToString() + "','" + ds.Rows[i]["RecorderID"].ToString() + "','" + ds.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                scon.Open();

                command.ExecuteNonQuery();
                scon.Close();




                SqlCommand command33 = new SqlCommand("INSERT INTO [HealthEvaluation_log] (CID,CaseNo,HealthEvalCode,RecorderID,EvalDateTime) VALUES('" + ds.Rows[i]["CID"].ToString() + "','" + ds.Rows[i]["CaseNo"].ToString() + "','" + ds.Rows[i]["HealthEvalCode"].ToString() + "','" + ds.Rows[i]["RecorderID"].ToString() + "','" + ds.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                scon.Open();

                command33.ExecuteNonQuery();
                scon.Close();





            }
        }









        //อ่านว่ามีข้อมูลเก่่าไหมถ้ามีก็อ่านทับ
        public void BindDataread()
        {



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtedu2 = new DataTable();


            SqlCommand command2 = new SqlCommand("select b.Name as DisabilityCode from [DisabilityEvaluation] a join DisabilityType b on a.DisabilityCode=b.DisabilityCode where CID=@CID and CaseNo=@CaseNo", scon);
           
            command2.Parameters.AddWithValue("@CID", Label1.Text);
            command2.Parameters.AddWithValue("@CaseNo", Label4.Text);
            command2.ExecuteNonQuery();
         



            SqlDataReader reader8 = command2.ExecuteReader();

            dtedu2.Columns.Add("DisabilityCode");
       
            while (reader8.Read() != false)
            {
                dtedu2.Rows.Add(reader8["DisabilityCode"].ToString());
       

            }
            reader8.Close();


            for (int j = 0; j < dtedu2.Rows.Count; j++)
            {
                string a = dtedu2.Rows[j]["DisabilityCode"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CBX");
                 
                    if (a == b)
                    {

                        
                        chk.Checked = true;
                    }
                }
            }


            scon.Close();







        }











        public void BindDataread2()
        {



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtedu = new DataTable();


            SqlCommand command = new SqlCommand("select b.Name as HealthEvalCode from [HealthEvaluation] a join HealthProblem b on a.HealthEvalCode=b.HealthEvalCode where CID=@CID and CaseNo=@CaseNo", scon);

            command.Parameters.AddWithValue("@CID", Label1.Text);
            command.Parameters.AddWithValue("@CaseNo", Label4.Text);
            command.ExecuteNonQuery();




            SqlDataReader reader = command.ExecuteReader();

            dtedu.Columns.Add("HealthEvalCode");

            while (reader.Read() != false)
            {
                dtedu.Rows.Add(reader["HealthEvalCode"].ToString());
              

            }
            reader.Close();


            for (int j = 0; j < dtedu.Rows.Count; j++)
            {
                string a = dtedu.Rows[j]["HealthEvalCode"].ToString();
                foreach (GridViewRow GR in GridView2.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CBKX");

                    if (a == b)
                    {

                       
                        chk.Checked = true;
                    }
                }
            }


            scon.Close();







        }



    }
}